package com.huaqin.obeacons.adapter;

import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaqin.obeacons.MessageListFragment;
import com.huaqin.obeacons.MessageViewActivity;
import com.huaqin.obeacons.R;
import com.huaqin.obeacons.utils.MyDatabaseUtil;

public class MainListViewAdapter extends BaseAdapter implements
		MessageListFragment.MessageListener {
	private int[] imgRes;
	private int[] imgRes2;
	private String[] desStr;
	private String[] cate;
	private Context context;
	private LayoutInflater mInflater;
	private SharedPreferences set;
	private String str;
	private List<Map<String, Object>> list;
	private MessageListFragment messageListFragment;

	public MainListViewAdapter(Context paramContext,
			MessageListFragment fragment) {
		this.context = paramContext;
		imgRes = new int[] { R.drawable.header_icon_company, R.drawable.header_icon_app , R.drawable.header_icon_location,R.drawable.header_icon_other};

		MyDatabaseUtil.getInstance(context);
		list = MyDatabaseUtil.queryDatafornews();
		messageListFragment = fragment;
		messageListFragment.setListener(this);

		this.mInflater = LayoutInflater.from(paramContext);
	}

	public int getCount() {
		if (list != null && list.size() != 0) {
			return list.size();
		}
		return 0;
	}

	public void updateData(){
		MyDatabaseUtil.getInstance(context);
		list = MyDatabaseUtil.queryDatafornews();
	}
	
	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder localViewHolder = null;
		if (convertView == null) {
			localViewHolder = new ViewHolder();
			convertView = this.mInflater.inflate(R.layout.list_item, null);
			localViewHolder.img = ((ImageView) convertView
					.findViewById(R.id.iv_image));
			localViewHolder.title = ((TextView) convertView
					.findViewById(R.id.item_title));
			localViewHolder.des = ((TextView) convertView
					.findViewById(R.id.item_des));
			convertView.setTag(localViewHolder);
		} else {
			localViewHolder = (ViewHolder) convertView.getTag();
		}
		if (list != null && list.size() != 0) {
			String type = list.get(position).get("type").toString();
			if (type.equals("1")) { // 公告消息
				localViewHolder.img.setImageResource(this.imgRes[0]);
			} else {
				localViewHolder.img.setImageResource(this.imgRes[1]);
			}
			localViewHolder.title.setText(list.get(position).get("title")
					.toString());
			localViewHolder.des.setText(this.list.get(position).get("content")
					.toString());
		}
		return convertView;
	}

	public class ViewHolder {
		TextView des;
		ImageView img;
		TextView title;
	}

	@Override
	public void notifyDialog(Context context,int position) {
		if (list != null && list.size() != 0) {
			String id = list.get(position).get("id").toString();
			String title = list.get(position).get("title").toString();
			String content = list.get(position).get("content").toString();
			String time = list.get(position).get("time").toString();
			Intent intent = new Intent(context, MessageViewActivity.class);
			intent.putExtra("id", id);
			intent.putExtra("content", content);
			intent.putExtra("title", title);
			intent.putExtra("time", time);
			context.startActivity(intent);
		}
	}
}
